const search = fxj.getSearch();
const vm = new Vue({
    el: '#main',
    data: {
        aid: search.get('aid'),
        address: decodeURIComponent(search.get('address')),
        title: decodeURIComponent(search.get('title')),
        img: decodeURIComponent(search.get('img')),
        date: '',
        name: '',
        tel: '',
    },
    methods: {},
});

function apiready() {
    fxj.fixLayout();
    initDataPicker();
}

function initDataPicker() {
    const today = getTodayDate();
    const dataEl = document.getElementById('dataEl');
    const month = (() => {
        const tempMonth = [];
        for (let i = 1; i <= 12; i++) {
            tempMonth.push({
                text: `${i}月`,
                value: i,
            });
        }
        return tempMonth;
    })();
    const day = createDayList(1);
    const house = createHouse();
    const picker = new Picker({
        data: [month, day, house],
        selectedIndex: [today.month - 1, today.day - 1, 0],
        title: '选择保洁时间',
    });
    picker.on('picker.select', function (selectedVal, selectedIndex) {
        vm.date = month[selectedIndex[0]].text + ' ' + day[selectedIndex[1]].text + ' ' + house[selectedIndex[2]].text;
        vm.dateInt = Number(new Date(`${today.year}/${month[selectedIndex[0]].value}/${day[selectedIndex[1]].value} ${house[selectedIndex[2]].value}`));
    });

    picker.on('picker.change', function (index, selectedIndex) {
        if (index === 0) {
            picker.refillColumn(1, createDayList(month[selectedIndex].value));
        }
    });

    dataEl.addEventListener('click', function () {
        picker.show();
    });
}

function getDayByMonth(month) {
    const today = getTodayDate();
    switch (month) {
        case 4:
        case 6:
        case 9:
        case 11:
            return 30;
        case 2:
            return today.year % 4 === 0 ? 29 : 28;
        default:
            return 31;
    }
}

function createDayList(month) {
    const dayNum = getDayByMonth(month);
    const dayList = [];
    for (let i = 1; i <= dayNum; i++) {
        dayList.push({
            text: `${i}号`,
            value: i,
        });
    }
    return dayList;
}

function createHouse() {
    const tempHouse = [];
    for (let i = 0; i <= 24; i++) {
        tempHouse.push(...[{
            text: `${i}:00`,
            value: `${i}:00`,
        }, {
            text: `${i}:30`,
            value: `${i}:30`,
        }]);
    }
    return tempHouse;
}

function getTodayDate() {
    const today = new Date();
    const year = today.getFullYear();
    const month = today.getMonth() + 1;
    const day = today.getDate();
    const week = today.getDay();
    getTodayDate = function () {
        return {
            year,
            month,
            day,
            week,
        };
    };
    return {
        year,
        month,
        day,
        week,
    };
}

async function submit() {
    const res = await ajax({
        url: 'home/CleaningService/index',
        data: {
            name: vm.name,
            phone: vm.tel,
            time: vm.dateInt.toString().slice(0, -3),
            aid: vm.aid,
        },
    });
    toast(res.msg);
    if (res.err === 0) {
        setTimeout(() => {
            fxj.closeWin();
            api.execScript({
                name: 'landlord_steward',
                script: 'reqHouseList()',
            });
        }, 2000);
    }
}